<?php
if(!isset($_SESSION)) 
	{ 
		session_start(); 
	}
function getlang(){
		$lang = $_SESSION['lang'];
		if (empty($lang) || $lang == "EN") {
			$value = "../value/value_en.php";
		} else if($lang == "VN") {
			$value = "../value/value_vn.php";
		}
		else {
			$value = "../value/value_kr.php";
		}
		return $value;
	}
include '../connection/connection.php';	
$value = getlang();
$list=$_SESSION['sort'];
include($value);
	$page=$_GET['pages'];
	$str=$_GET['string'];
	$_SESSION['sortn']=$str;
	$flagDesc=$_GET['flagDesc'];

	$datecurrent = date("Y-m-d");
	$rows_per_page = 20 ;
	$page_start = ( $page - 1 ) * $rows_per_page; 
	$page_end = $page * $rows_per_page;
	if($flagDesc==1){

		if($list[$str]=="DESC")
			$list[$str]="ASC";
		else
			$list[$str]="DESC";
	}
	
	
	$sql = "select m.*,n.* from (select ss.* from (SELECT lv.*,ui.email
FROM `user_info` ui, log_viewer lv 
WHERE lv.id = ui.id AND lv.ins_time >'".$datecurrent." 00:00:00' AND lv.ins_time<'".$datecurrent." 23:59:59' 

ORDER BY ins_time DESC) as ss GROUP BY ss.id) as m,

(SELECT aa.* from (SELECT user_info.id,
(SELECT count(log_viewer.id) from log_viewer where user_info.id=log_viewer.id and log_viewer.`status`='login' 
		and log_viewer.ins_time >'".$datecurrent." 00:00:00' AND log_viewer.ins_time<'".$datecurrent." 23:59:59' ) as login,
(SELECT count(log_viewer.id) from log_viewer where user_info.id=log_viewer.id and log_viewer.`status`='logout' 
		and log_viewer.ins_time >'".$datecurrent." 00:00:00' AND log_viewer.ins_time<'".$datecurrent." 23:59:59') as logout 
from user_info) as aa WHERE aa.login!=0) as n 
WHERE m.id=n.id";
	$selectuserlogin = mysql_query($sql." ORDER BY ".$str." ".$list[$str]);
	$number_of_page = ceil ( mysql_num_rows( $selectuserlogin ) / $rows_per_page ); 
	
	$_SESSION['sort']=$list;
?>
<thead>
				<tr>
					<td align="center" style="width: 4%"><?php echo $no?></td>
					<td align="center"><a onclick="pagingLogindayUser('<?=$page?>','email',1)" style="cursor: pointer;color: #027acc"><?php echo $email ?></a></td>
					<td align="center"><a onclick="pagingLogindayUser('<?=$page?>','ins_time',1)" style="cursor: pointer;color: #027acc"><?php echo $logintime ?></a></td>
					<td align="center"><a onclick="pagingLogindayUser('<?=$page?>','login',1)" style="cursor: pointer;color: #027acc"><?php echo $logintimes?></a></td>
					<td align="center"><a onclick="pagingLogindayUser('<?=$page?>','email',1)" style="cursor: pointer;color: #027acc"><?php echo $logouttimes?></a></td>
				</tr>
			</thead>
			<?php
			if(mysql_num_rows($selectuserlogin)>0){
				$i=0;
				while ($row = mysql_fetch_array($selectuserlogin)){
					if ( $i >= $page_start ){?>
				<tr>
						<td align="center" style="width: 4%;color: #696969;"><?php echo $i+1?></td>
						<td align="center"><a onclick="onclickDetailEmailLoginInDay(<?=$row[id]?>,'<?=$row[email]?>');" style="color: #0099FF;cursor: pointer;"><?php echo $row[email] ?></a></td>
						<td align="center"><?php echo $row[ins_time] ?></td>
						<td align="center"><?php echo $row[login]?></td>
						<td align="center"><?php echo $row[logout]?></td>
					</tr>
			<?php
					}
						$i++;
						if ($i >= $page_end){
							break;
						}
					}
				}
			?>
			
			<tr>
						<td colspan="8" class="rf-dt-shdr-c">
							<span style="padding:1px; display: inline-block;">
														
								<?php if ( $number_of_page > 1 ) 
								{ 
									if ($page==1)
										$p=2;
									else if ($page==$number_of_page)
										$p=$number_of_page-1;
									else $p=$page;
									if($page!=1){?>
										<a onclick="pagingLogindayUser(<?=1?>,'<?=$str?>',0)"><span class='button_next'><?php echo "<<<";?></span> </a>
										<a onclick="pagingLogindayUser(<?=($page-1)?>,'<?=$str?>',0)"><span class='button_next'><?php echo "<<";?></span> </a>
									<?php  }
									else{
										echo "<span class='button_next'><<<</span> "; 
										echo "<span class='button_next'><<</span> ";
									}
									for ( $i = $p-1; $i <= $p+1; $i++ ) {	
											if ($i<=$number_of_page && $i>0)
											
												if ($i==$page){
													echo "<span class='button_next'><b>".$i."</b></span>"; 
												}else{
												?>
													<a onclick="pagingLogindayUser(<?=$i?>,'<?=$str?>',0)"><span class="button_next">
													<?php 
													echo $i;
													?>
													</span> </a>
													<?php 
												}
											?>
										
									<?php 
											}
										
										if($page!=$number_of_page){ ?>
											<a onclick="pagingLogindayUser(<?=($page+1)?>,'<?=$str?>',0)"><span class='button_next'><?php echo ">>";?></span> </a>
											<a onclick="pagingLogindayUser(<?=($number_of_page)?>,'<?=$str?>',0)"><span class='button_next'><?php echo ">>>";?></span> </a>
										<?php }
										else{
											echo "<span class='button_next'>>></span> ";
											echo "<span class='button_next'>>>></span> ";
										}
									}
								
									
									?>
																						
							</span>
						</td>
					</tr>